<?php

class Common_Cron_Executor_ChangePassword extends Common_Cron_Executor {

    public $className = 'changePassword';

    public function exec(Zend_Db_Table_Row &$task) {


        $controller = $this->getController();
        $view = new Zend_View();
        $view->setBasePath(APPLICATION_PATH . '/layouts/mail');
        $json = json_decode($task->input);
        
        $view->link = 'http://' . Zend_Registry::get('config')->domain
                . '/'
                . 'index/task/code/' . $json->code.'/id/'.$task->id;
        
        $body = $view->render('' . $task->class . '.phtml');

        $controller->sendMail(array(
            'body' => $body,
            'to' => array(
                $json->email=> !empty($json->name) ? $json->name : 'name of user'
            )
        ));

        $task->status = Application_Model_Tasks::S_WTOKU;       
        $task->output = 'ok';
        $task->save();
    }

    public function addTask($data) {

        $tasksmodel = new Application_Model_Tasks();

        $tasksmodel->insert(array(
            'status' =>$data['status'],
            'objectId' => $data['objectId'],
            'table' => 'users',
            'output' => '',
            'created' => $data['created'],
            'input' => $data['input'],
            'class' => $data['class']
        ));
        
    }
    
    public function completeTask(Zend_Db_Table_Row &$task) {
        
        $users = new Application_Model_Users();
        $json = json_decode($task->input);
        
        $user = $users->getById($task->objectId);
            
        $user->password = $json->newpassword;
        $user->save();
        
        $task->status = Application_Model_Tasks::S_OK;
        $task->completed = date("Y-m-d H:i:s",  time());        
        $task->output = 'ok';
        $task->save();
        
    }

}